<?php
include("design.php");
menu();

if (!empty ($_POST['Pridej']))

{
    $err = array();
    if (!is_string($_POST['n'])||count($_POST['n'])<1){
		$err['n'] = 'Špatné jméno.';
	}
    if (!is_numeric($_POST['v']) || $_POST['v']<=0){
		$err['v'] = 'Špatná vyska.';
	}
	    	
	if (!is_numeric($_POST['m']) || $_POST['m']<=0){
		$err['m'] = 'Špatná váha.';
	}
    
}   

$showForm = false;


if (isset($err)){
        if (!empty($err)){
                $showForm = true;
        
                foreach ($err as $current){
                        echo "<div class='WrongValue'>" . $current . "</div><br/>";
                }
        }
                
}

$conn= mysql_connect("localhost",  "suchapa3",  "ool9eiy3" );
        if (!$conn) { 
                                die(' Připojení k databázi bylo neúspené: ' . mysql_error()); 
        }
                mysql_query("SET CHARACTER SET utf8;");
                mysql_query("SET NAMES utf8");
                mysql_select_db("suchapa3");

if(isset($_POST['m'])&&(!showform)){
$bmi = $_POST['m'] /($_POST['v'] * $_POST['v']/10000);


$datum = Date("Y-m-d");
$query = "INSERT INTO BMIvys (Date, BMI)
                        VALUES ('".$datum."','".$bmi."')";
$rs = mysql_query($query);
if (!$rs) {
                    echo "Chyba v 1. query: $query";
                        return false;
                   }
  else{
  echo"hotovo";}
                  

}


if ((!empty($_POST['year']))&&(!empty($_POST['month']))&&(!empty($_POST['day']))){
        $err2 = array();
        
                
        if (!is_numeric($_POST['month']) || $_POST['month']>12|| $_POST['month']<=0){
                $err2['month'] = 'Špatně zadaný měsíc.';
        }
                
        if (!is_numeric($_POST['year'])){
                $err2['year'] = 'Špatně zadaný rok.';
        }
        
                  
        if (!is_numeric($_POST['day']) || $_POST['day']<=0|| $_POST['day']>31){
                $err2['day'] = 'Špatně zadaný den.';
        }
        
}




if (isset($err2)){
        if (!empty($err2)){
                
        
                foreach ($err2 as $current){
                        echo "<div class='WrongValue'>" . $current . "</div><br/>";
                }
        }else{
        $SetDate = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
        $SetDate= strtotime($SetDate);
                $SetDate= date('Y-m-d',$SetDate);
    

        }
        
}


?>
<form action="overview.php" method="post">
                
        <br/><br/>
        <label for="day">Den</label>
        <input type="text" id="day" name="day" value="<?php echo $_POST['day'];?>">
        <br/><br/>
        <label for="month">Měsíc </label>
        <input type="text" id="month" name="month" value="<?php echo $_POST['month'];?>">
    <br/><br/>
        <label for="year">Rok</label>
        <input type="text" id="year" name="year" value="<?php echo $_POST['year'];?>">
        <br/><br/>
        <input type="submit" value="Zobraz">
</form> 
           
<?php


if(!empty($SetDate)){
$query ="SELECT Date,Bmi FROM BMIvys WHERE Date > '$SetDate'";
}
else{
$query ="SELECT Date,Bmi FROM BMIvys";}                  
                        
$rs = mysql_query($query);
if (!$rs) {
                    echo "Chyba v 3. query: $query";
                    return false;
                        }     
mysql_close();
echo "<table><tr><th>Datum</th><th>BMI</th></tr>";
        
                        
while ($current=mysql_fetch_array($rs)){ 
                        echo "<tr>";
                        echo "<td>";
                        echo $current["Date"];
                        echo "</td>";
                        echo "<td>";
                        echo number_format($current["Bmi"],2);
                        echo "</td>";
                        echo "</tr>";                   
                }
                
                echo "</table>";
 ShowFooter();
?>




</body>
</html>